package com.mdd.maduoduo.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;

import java.util.Locale;

public class ManufacturerUtils {

    private static final String TAG = "ManufacturerUtils";

    public static void applyManufacturerSettings(Context context) {
        if (context == null) return;

        String manufacturer = Build.MANUFACTURER.toLowerCase(Locale.ROOT);
        Log.d(TAG, "Applying settings for manufacturer: " + manufacturer);

        try {
            // 通用设置
            requestIgnoreBatteryOptimizations(context);

            // 厂商特定设置
            if (manufacturer.contains("xiaomi")) {
                applyXiaomiSettings(context);
            } else if (manufacturer.contains("huawei") || manufacturer.contains("honor")) {
                applyHuaweiSettings(context);
            } else if (manufacturer.contains("oppo")) {
                applyOppoSettings(context);
            } else if (manufacturer.contains("vivo")) {
                applyVivoSettings(context);
            } else if (manufacturer.contains("samsung")) {
                applySamsungSettings(context);
            } else if (manufacturer.contains("meizu")) {
                applyMeizuSettings(context);
            }
        } catch (Exception e) {
            Log.e(TAG, "Apply manufacturer settings failed", e);
        }
    }

    private static void requestIgnoreBatteryOptimizations(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                if (pm != null && !pm.isIgnoringBatteryOptimizations(context.getPackageName())) {
                    Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                    intent.setData(Uri.parse("package:" + context.getPackageName()));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
            } catch (Exception e) {
                Log.e(TAG, "Request ignore battery optimizations failed", e);
            }
        }
    }

    private static void applyXiaomiSettings(Context context) {
        try {
            // 自启动管理
            openActivity(context,
                    "com.miui.securitycenter",
                    "com.miui.permcenter.autostart.AutoStartManagementActivity");

            // 电池优化
            openActivity(context,
                    "com.miui.powerkeeper",
                    "com.miui.powerkeeper.ui.HiddenAppsConfigActivity");

            // 应用锁
            openActivity(context,
                    "com.miui.securitycenter",
                    "com.miui.appmanager.AppManagerMainActivity");

        } catch (Exception e) {
            Log.e(TAG, "Apply Xiaomi settings failed", e);
        }
    }

    private static void applyHuaweiSettings(Context context) {
        try {
            // 启动管理
            openActivity(context,
                    "com.huawei.systemmanager",
                    "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity");
            return;
        } catch (Exception e1) {
            try {
                // 启动管理
                openActivity(context,
                        "com.huawei.systemmanager",
                        "com.huawei.systemmanager.optimize.process.ProtectActivity");
                return;
            } catch (Exception e2) {

            }
        }
        try {
            openActivity(context,"com.huawei.systemmanager",
                    "com.huawei.systemmanager.power.ui.HwPowerManagerActivity");
        } catch (Exception e1) {
            try {
                // 方式2：旧版本华为
                openActivity(context,"com.huawei.systemmanager",
                        "com.huawei.systemmanager.power.ui.PowerManagementActivity");
            } catch (Exception e2) {
            }
        }

//        try {
//            // 启动管理
//            openActivity(context,
//                    "com.huawei.systemmanager",
//                    "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity");

//            // 电池优化
//            openActivity(context,
//                    "com.huawei.systemmanager",
//                    "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity");
//
//        } catch (Exception e) {
//            Log.e(TAG, "Apply Huawei settings failed", e);
//        }
    }

    private static void applyOppoSettings(Context context) {
        try {
            // 自启动管理
            openActivity(context,
                    "com.coloros.safecenter",
                    "com.coloros.safecenter.permission.startup.StartupAppListActivity");

            // 电池优化
            openActivity(context,
                    "com.coloros.oppoguardelf",
                    "com.coloros.powermanager.fuelgaue.PowerUsageModelActivity");

        } catch (Exception e) {
            Log.e(TAG, "Apply Oppo settings failed", e);
        }
    }

    private static void applyVivoSettings(Context context) {
        try {
            // 自启动管理
            openActivity(context,
                    "com.vivo.abe",
                    "com.vivo.applicationbehaviorengine.ui.ExcessivePowerManagerActivity");

            // 电池优化
            openActivity(context,
                    "com.vivo.abe",
                    "com.vivo.abe.ui.AppPowerManagerActivity");

        } catch (Exception e) {
            Log.e(TAG, "Apply Vivo settings failed", e);
        }
    }

    private static void applySamsungSettings(Context context) {
        try {
            // 电池优化
            openActivity(context,
                    "com.samsung.android.sm",
                    "com.samsung.android.sm.ui.battery.BatteryActivity");

        } catch (Exception e) {
            Log.e(TAG, "Apply Samsung settings failed", e);
        }
    }

    private static void applyMeizuSettings(Context context) {
        try {
            // 自启动管理
            openActivity(context,
                    "com.meizu.safe",
                    "com.meizu.safe.permission.SmartBGActivity");

        } catch (Exception e) {
            Log.e(TAG, "Apply Meizu settings failed", e);
        }
    }

    private static void openActivity(Context context, String pkg, String cls) {
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(pkg, cls));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {
            Log.e(TAG, "Open activity failed: " + pkg + "/" + cls, e);
        }
    }
}
